
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/view/pop_pages/alarm_info_page.dart';
import 'package:huxiaodui/view/pop_pages/alarm_message_list.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class AlarmList extends StatelessWidget {
  final List<AlarmModel> alarms;
  const AlarmList({super.key, required this.alarms});

  @override
  Widget build(BuildContext context) {
    for (AlarmModel element in alarms) {
      element.reload();
    }
    getLine(String text) {
      return Container(
        padding: const EdgeInsets.only(top: 1, bottom: 1),
        width: 200,
        child: Text(
          text,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      );
    }

    Widget alarmUi(int index) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            children: [
              getLine("报警单位：${alarms[index].unit.name}"),
              getLine("报警类型：一键报警"),
              getLine("报警时间：${DateFormat('yyyy-MM-dd HH:mm:ss').format(alarms[index].alarmAt)}"),
              getLine("定位更新时间：${alarms[index].locationAtDistanceToString}"),
              getLine("持续时长：${alarms[index].durationToString}"),
              const SizedBox(height: 10),
              getLine("负责人：${alarms[index].unit.manager}"),
              getLine("联系电话：${alarms[index].unit.tel}"),
              getLine("地址：${alarms[index].unit.addr}"),
              getLine("辖区派出所：${alarms[index].unit.policeStation}"),
            ],
          ),
          Column(
            children: [
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => AlarmMessageInfos(
                        alarm: alarms[index],
                      ),
                    ),
                  );
                },
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.blue),
                ),
                child: const Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Icon(Icons.camera_rear_outlined),
                    Text("详情"),
                  ],
                ),
              ),
              Visibility(
                visible: !alarms[index].expired,
                child: ElevatedButton(
                  onPressed: () {
                    MapController().toMarker(alarms[index]);
                  },
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.cyan),
                  ),
                  child: const Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Icon(Icons.add_circle_outline),
                      Text("查看"),
                    ],
                  ),
                ),
              ),
              // Visibility(
              //   visible: !alarms[index].expired,
              //   child: ElevatedButton(
              //     onPressed: () {
              //
              //     },
              //     style: ButtonStyle(
              //       backgroundColor:
              //       MaterialStateProperty.all(Colors.lightBlueAccent),
              //     ),
              //     child: const Row(
              //       children: [
              //         Icon(Icons.info_outline),
              //         Text("详情"),
              //       ],
              //     ),
              //   ),
              // ),
            ],
          ),
        ],
      );
    }

    return ListView.builder(
      itemCount: alarms.length,
      itemBuilder: (BuildContext context, int index) {
        return InkWell(
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => AlarmInfoPage(alarm: alarms[index])),
            );
          },
          child: Container(
            padding:
                const EdgeInsets.only(top: 10, bottom: 10, left: 10, right: 0),
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 0.3),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        const Text(
                          "【一键报警】",
                          style: TextStyle(color: Colors.redAccent),
                        ),

                        Text(
                          alarms[index].alarmAt.toString().split('.')[0],
                          style: TextStyle(color: Colors.blue),
                        ),
                        if(alarms[index].alarmAtDistance.inDays < 30)Text(
                          ' | ',
                          style: const TextStyle(fontWeight: FontWeight.w700),
                        ),
                        if(alarms[index].alarmAtDistance.inDays < 30)Text(
                          '${alarms[index].alarmAtDistanceToString}',
                          style: const TextStyle(color: Colors.blueAccent,fontWeight: FontWeight.w700),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        Text(
                          alarms[index].durationToString,
                          style: TextStyle(color: Colors.blueAccent),
                        ),
                        const Icon(
                          Icons.timelapse,
                          color: Colors.blueAccent,
                        ),
                      ],
                    )
                  ],
                ),
                Text(
                  alarms[index].unit.name,
                  style: const TextStyle(fontSize: 16),
                ),

                alarms[index].expired ?
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    ElevatedButton(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => AlarmMessageInfos(
                              alarm: alarms[index],
                            ),
                          ),
                        );
                      },
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(Colors.blue),
                      ),
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(Icons.camera_rear_outlined),
                          Text("警情"),
                        ],
                      ),
                    ),
                    SizedBox(width: 10),
                  ],
                ): Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    ElevatedButton(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => AlarmMessageInfos(
                              alarm: alarms[index],
                            ),
                          ),
                        );
                      },
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(Colors.blue),
                      ),
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(Icons.camera_rear_outlined),
                          Text("警情"),
                        ],
                      ),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        MapController().toMarker(alarms[index]);
                      },
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(Colors.cyan),
                      ),
                      child: const Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(Icons.add_circle_outline),
                          Text("查看"),
                        ],
                      ),
                    ),

                  ],
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
